#include "bdd.h"

byte
td_update_ident_row (unsigned int si, char *se, char *nid, byte mode)
{
  session *stmp;
  row *rtmp;
  td_err = 0;
  stmp = get_session (si);
  if (!stmp)
    {
      td_err = INVALID_SESSION;
      return 0;
    }
  if (!stmp->subptr)
    {
      td_err = INVALID_SUBBASE;
      return 0;
    }

  if (!valid_string (nid))
    {
      td_err = INVALID_STRING;
      return 0;
    }
    
  if (get_row (stmp->subptr->rows, nid, stmp->rootptr->case_flag))
    {
      td_err = ALREADY_EXIST;
      return 0;
    }

  if (mode == SELECTED)
    {
      if (!stmp->rowptr)
	return INVALID_ROW;
      update_ident_row (stmp->rowptr, nid);
      if (stmp->rootptr->mode == O_AUTO)
	write_bdd (stmp->baseptr);
      return 1;
    }

  if (!valid_string (se))
  {
      td_err = INVALID_STRING;
      return 0;
  }
    
  rtmp = get_row (stmp->subptr->rows, se, stmp->rootptr->case_flag);
  if (!rtmp)
    {
      td_err = INVALID_ROW;
      return 0;
    }
  update_ident_row (rtmp, nid);

  if (stmp->rootptr->mode == O_AUTO)
    write_bdd (stmp->baseptr);

  return 1;
}

byte
td_update_ident_row_p (unsigned int si, char *se, char delim, char *nid)
{
  td_row *n = NULL;
  n = td_select_row_p (si, se, delim);
  if (n)
    td_free_row (n);
  else
    return (0);
  return (td_update_ident_row (si, se, nid, SELECTED));
}

byte
td_update_val_row (unsigned int si, char *se, char *nval, byte mode)
{
  session *stmp;
  row *rtmp;
  td_err = 0;
  stmp = get_session (si);
  if (!stmp)
    {
      td_err = INVALID_SESSION;
      return 0;
    }
  if (!stmp->subptr)
    {
      td_err = INVALID_SUBBASE;
      return 0;
    }

  if (!valid_string (nval))
    {
      td_err = INVALID_STRING;
      return 0;
    }
    
  if (mode == SELECTED)
    {
      if (!stmp->rowptr)
	{
	  td_err = INVALID_ROW;
	  return 0;
	}
      update_val_row (stmp->rowptr, nval);
      if (stmp->rootptr->mode == O_AUTO)
	write_bdd (stmp->baseptr);
      return 1;
    }

    if (!valid_string (se))
    {
      td_err = INVALID_STRING;
      return 0;
    }
    
  rtmp = get_row (stmp->subptr->rows, se, stmp->rootptr->case_flag);
  if (!rtmp)
    {
      td_err = INVALID_ROW;
      return 0;
    }
  update_val_row (rtmp, nval);
  if (stmp->rootptr->mode == O_AUTO)
    write_bdd (stmp->baseptr);
  return 1;
}

byte
td_update_val_row_p (unsigned int si, char *se, char delim, char *nval)
{
  td_row *n = NULL;
  n = td_select_row_p (si, se, delim);
  if (n)
    td_free_row (n);
  else
    return (0);
  return (td_update_val_row (si, se, nval, SELECTED));
}


byte
td_update_ident_subbase (unsigned int si, char *nid)
{
  session *stmp;
  td_err = 0;
  stmp = get_session (si);
  if (!stmp)
    {
      td_err = INVALID_SESSION;
      return 0;
    }
  if (!stmp->subptr || stmp->subptr == stmp->baseptr)
    {
      td_err = INVALID_SUBBASE;
      return 0;
    }

  if (!valid_string (nid))
    {
      td_err = INVALID_STRING;
      return 0;
    }
    
  if (stmp->subptr->parent)
    if (get_subbase
	(stmp->subptr->parent->enfant, nid, stmp->rootptr->case_flag))
      {
	td_err = ALREADY_EXIST;
	return 0;
      }

  update_ident_subbase (stmp->subptr, nid);
  if (stmp->rootptr->mode == O_AUTO)
    write_bdd (stmp->baseptr);
  return 1;
}
